package com.baijiaoxi.common.utils.jdk;

public class NameCastUtil {
    /**
     * 转为驼峰且首字母大写
     *
     * @param s
     * @return
     */
    public static String toCamelCaseUpperFirst(String s) {
        return toCamel(s, true);
    }

    /**
     * 转为驼峰且首字母小写
     *
     * @param s
     * @return
     */
    public static String toCamel(String s) {
        return toCamel(s, false);
    }

    /**
     * 下划线或减号转驼峰
     *
     * @param s
     * @param upperFirst
     * @return
     */
    private static String toCamel(String s, boolean upperFirst) {
        if (s == null) {
            return null;
        }
        s = s.toLowerCase();
        StringBuilder sb = new StringBuilder(s.length());
        boolean upperCase = upperFirst;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '_' || c == '-') {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(c));
                upperCase = false;
            } else {
                sb.append(Character.toLowerCase(c));
            }
        }
        return sb.toString();
    }

    /**
     * 转为 AAA-BBB形式
     *
     * @param str
     * @return
     */
    public static String toUpperMinus(String str) {
        return toSplitCase(str, "-", true);
    }

    /**
     * 转为 aaa-bbb形式
     *
     * @param str
     * @return
     */
    public static String toLowerMinus(String str) {
        return toSplitCase(str, "-", false);
    }

    /**
     * 转为 aaa_bbb形式
     *
     * @param str
     * @return
     */
    public static String toLowerScore(String str) {
        return toSplitCase(str, "_", false);
    }

    /**
     * AAA_BBB
     *
     * @param str
     * @return
     */
    public static String toUpperScore(String str) {
        return toSplitCase(str, "_", true);
    }

    /**
     * 驼峰转分隔形式
     *
     * @param str   要转换的字符串
     * @param split 分隔符
     * @param upper 转换后是否全部大写，false全部小写
     * @return
     */
    private static String toSplitCase(String str, String split, boolean upper) {
        if (str == null)
            return null;
        StringBuilder sb = new StringBuilder();
        // 前置字符是否大写
        boolean preCharIsUpperCase = true;
        // 当前字符是否大写
        boolean curreCharIsUpperCase = true;
        // 下一字符是否大写
        boolean nexteCharIsUpperCase = true;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (i > 0) {
                preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
            } else {
                preCharIsUpperCase = false;
            }

            curreCharIsUpperCase = Character.isUpperCase(c);

            if (i < (str.length() - 1))
                nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));


            if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase)
                sb.append(split);
            else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase)
                sb.append(split);
            sb.append(upper ? Character.toUpperCase(c) : Character.toLowerCase(c));
        }

        return sb.toString();
    }
}
